package com.wzh.glsc.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
//import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

//跨域配置
//该注解代表这是个注册类
@Configuration
public class GlscCorsConfiguration  {
    //CorsWebFilter 返回跨域的Filter
    //@Bean 加入容器中
    @Bean
    public CorsWebFilter corsWebFilter(){
        //基于url跨域的配置
        UrlBasedCorsConfigurationSource source=new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration=new CorsConfiguration();
        //允许所有请求头跨域
        corsConfiguration.addAllowedHeader("*");
        //允许所有请求方式跨域
        corsConfiguration.addAllowedMethod("*");
        //允许所有请求来源跨域
        corsConfiguration.addAllowedOriginPattern("*");
        //允许所有请求来源跨域
//        corsConfiguration.addAllowedOrigin("*");
        //是否允许携带cookie请求头跨域
        corsConfiguration.setAllowCredentials(true);
        //为所有路径 配置跨域
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsWebFilter(source);
    }
}
